Introduction to Nest.js

  1. Difference between NestJS and NodeJS

    NestJS is a framework that lets programmers focus on the application problem at hand rather than the tiny implementation details like setting up TypeScript, API routing, error handling, middleware setup, etc.

    Think of NestJS as a layer on top of NodeJS itself, abstracting away difficult tasks, tools and boilerplate code, and adding a complete toolkit for application development. NestJS uses express as the bottom layer by default, or you can choose Fastify or others (but you need to set up a compatible library).


  2. Before using NestJS

    First install NodeJS, version 12 or 13 at least. View in Terminal: node --version.

    Install cli: npm i -g @nestjs/cli.

    Check the version number of nest after installation: nest --version.


  3. Nest cli

    nest cli is a companion tool for nestJS, which can help us generate files, run, compile and even bundle our applications. It contains many commands, which can be viewed with nest --help.


  4. Create your first nest application

    In Terminal, enter nest new. Then follow the prompts to select the name and package manager. After creation, enter cd new file name, npm run start, you can open and browse to localhost: 3000 to open the file.


  5. Nest folder structure

    Inside the Nest app, everything has a defined structure. In the folder, there are many files that are familiar before, but be aware of a specific unique file called Nest cli json .

    The entry file of nest is the main.ts file under src. Nest files are created with NestFactory.create(AppModule). AppModule is the root module of our application and contains everything our application needs to run. It consists of many small modules that combine to form a complete application.

    After opening the app.module.ts file, you can see the decorator @Module(). Decorators can be used on classes or other methods. The first layer in @Module({ }) is the controllers and providers of the entry-level file app.modules.js, if not, delete it. After that, import: [] is used to load other modules. Folders for these modules are placed in the src directory.


  6. controller and service basic

    Controller is a class, but with the @Controller() decorator. Controllers are used to handle specific requests. The controller needs to use service to separate the logic.


    In the app.controller.ts file:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    @Controller()
    export class AppController{
    constructor(private readonly appService: AppService){}

    @Get()
    getHello(): string{
    return this.appService.getHello();
    }
    }

    In the app.service.ts file:

    1
    2
    3
    4
    5
    6
    @Injectable()
    exports class AppService {
    getHello(): string{
    return 'Hello World!';
    }
    }

  1. API test software

    Insomnia & Postman


  2. Start Nest in dev mode

    npm run start: dev. This mode integrates nodemon functionality.

Share